package com.yyq.sos.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yyq.adms.sysmanage.dao.SysModulesDao;
import com.yyq.adms.sysmanage.dao.impl.SysModulesDaoImpl;
import com.yyq.adms.sysmanage.pojo.SysModules;
import com.yyq.adms.usermanage.bpo.SysUsersBpo;
import com.yyq.servlets.ApplicationContextHandle;


public class SysModulesUtil
{
	private final static  Map map = new HashMap();
	private final static SysModulesUtil instance = new SysModulesUtil();
	
	private SysModulesDao dao;
	
	private SysModulesUtil(){
		//dao = new SysModulesDaoImpl();
		if(dao == null){
			dao = (SysModulesDaoImpl)ApplicationContextHandle.getBean("sysModulesDao");
		}
		List list = dao.search("from "+SysModules.class.getName()+" where sts='ACTV' and parentId<>0");
		for(int i=0; i<list.size(); ++i){
			SysModules obj = (SysModules)list.get(i);
			map.put(String.valueOf(obj.getModuleName()), obj);
		}
	}
	
	public static SysModulesUtil getInstance(){
		return instance;
	}
	
	public SysModules getContent(String moduleId){
		return (SysModules)map.get(moduleId);
	}
	
	public String getModuleName(String path){
		for(Iterator it = map.keySet().iterator(); it.hasNext();){
			Object obj = it.next();
			SysModules ms = (SysModules)map.get(obj);
			if(ms.getParentId().longValue()!=0){
				String url = ms.getModuleUrl();
				if(url.indexOf("action")<0 && url.length()<6)
					continue;
				url = url.substring(0,url.indexOf("action")+6);
				//System.out.println("path==============="+path);
				//System.out.println("url================"+url);
				if(path.indexOf(url)>0)
					return (String)obj;
			}
		}
		return null;
	}
	
	public String getModuleName(long moduleId){
		for(Iterator it = map.keySet().iterator(); it.hasNext();){
			Object obj = it.next();
			SysModules ms = (SysModules)map.get(obj);
			if(ms.getId().longValue()==moduleId){
				return ms.getModuleName();
			}
		}
		return null;
	}
	
	public void rebuildMap(){
		map.clear();
		List list = dao.search("from "+SysModules.class.getName()+" where sts='ACTV' and parentId<>0");
		for(int i=0; i<list.size(); ++i){
			SysModules obj = (SysModules)list.get(i);
			map.put(obj.getModuleName(), obj);
		}
	}
}